package com.imdroid.lite.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.imdroid.lite.IMDroid;
import com.imdroid.lite.R;
import com.imdroid.lite.model.User;
import com.imdroid.lite.process.ProcessFactory;
import com.imdroid.lite.process.UserProcess;

public class RegisterMain extends BaseActivity {
	
	private String mobile;
	private String verifyCode;
	private String pass;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reg_main);
	}
	
	public void back(View view){
		startActivity(new Intent(this, HomeLoginActivity.class));
		finish();
	}
	
	public void requestSubmit(View view){
		if(!checkInput())
			return;
		new RegisterUserTask(this, R.string.home_wait_title,
				R.string.home_wait_register).execute(new Object());
	}
	
	public void requestVerifyCode(View view){
		String phoneNumber = ((EditText) this
				.findViewById(R.id.mobile)).getText().toString();
		if (phoneNumber == null || phoneNumber.length() <= 0 || !phoneNumber.matches("^[1][0-9]{10}$")) {
			showAlert(R.string.remind, R.string.reg_tip_valid_mobile);
			return;
		}
		(new GetValidationCodeTask(this, R.string.home_wait_title,
				R.string.home_wait)).execute(phoneNumber);
	}
	
	private boolean checkInput(){
		mobile = ((EditText) findViewById(R.id.mobile)).getText().toString();
		verifyCode = ((EditText) findViewById(R.id.verify_code)).getText().toString();
		pass = ((EditText) findViewById(R.id.pass)).getText().toString();
		if(mobile == null || mobile.trim().length() == 0 || !(mobile.trim().matches("^[1][0-9]{10}$"))){
			showAlert(getString(R.string.reg_tip_valid_mobile));
			return false;
		}
		if(verifyCode == null || verifyCode.trim().length() <= 0){
			showAlert(getString(R.string.reg_tip_input_verify_code));
			return false;
		}
		if(pass == null || pass.trim().length() <= 0){
			showAlert(getString(R.string.reg_tip_password));
			return false;
		}
		return true;
	}

	protected class GetValidationCodeTask extends ProgressBarAsyncTask {
		/**
		 * The constructor
		 * 
		 * @param context
		 *            the application context
		 * @param titleText
		 *            the title text
		 * @param messageText
		 *            the message text
		 */
		public GetValidationCodeTask(BaseActivity context, int titleText,
				int messageText) {
			super(context, titleText, messageText);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.imdroid.lite.activities.BaseActivity.ProgressBarAsyncTask#
		 * doInBackground(java.lang.Object[])
		 */
		protected Exception doInBackground(Object... params) {
			UserProcess process = (UserProcess) ProcessFactory
					.getProcess(UserProcess.class);
			try {
				process.getNewRegisterCode((String) params[0]);
			} catch (Exception e) {
				return e;
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		protected void onPostExecute(Exception result) {
			super.onPostExecute(result);

			if (result == null) {
				context.showAlert(context.getString(R.string.home_reg_sms_send));
			} else {
				Toast.makeText(context, result.getMessage(), Toast.LENGTH_SHORT)
						.show();
			}
		}
	}
	
	/**
	 * The task of register the user
	 * 
	 */
	protected class RegisterUserTask extends ProgressBarAsyncTask {
		/**
		 * The constructor
		 * 
		 * @param context
		 *            the application context
		 * @param titleText
		 *            the title text
		 * @param messageText
		 *            the message text
		 */
		public RegisterUserTask(BaseActivity context, int titleText,
				int messageText) {
			super(context, titleText, messageText);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see com.imdroid.lite.activities.BaseActivity.ProgressBarAsyncTask#
		 * doInBackground(java.lang.Object[])
		 */
		protected Exception doInBackground(Object... params) {
			UserProcess process = (UserProcess) ProcessFactory
					.getProcess(UserProcess.class);

			// 判断是新加用户还是用户修改
			User user = new User();
			user.setUser_name(mobile);
			user.setNickname(mobile);
			user.setMobile_phone(mobile);
			user.setPassword(pass);
			try {
					// 在本机注册用户
					User currentUser = process.registerVistor(user,
							verifyCode);
					IMDroid.registerUser(currentUser);
			} catch (Exception ex) {
				return ex;
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		protected void onPostExecute(Exception result) {
			super.onPostExecute(result);
			if (result != null) {
				Toast.makeText(context, result.getMessage(), Toast.LENGTH_SHORT)
						.show();
				return;
			}
			startActivity(new Intent(RegisterMain.this,
					MainActivity.class));

			finish();
		}
	}
	
}
